package algorithm.t202111;

/**
 * @author : 李红磊
 * @version :1.0
 * @date : 2021/11/14 10:39
 * @description :
 */
public class t20211114 {

    //520.检测大写字母
    public boolean detectCapitalUse(String word) {
        int len = word.length();
        if (len == 1) return true;

        char firstLetter = word.charAt(0);

        if (firstLetter - 97 < 0) {//表明首字母为大写，若再出现大写就报错
            int num = len - 1;//走到这，分两种情况：一种是单词中的字母都为大写，另一种是只有首字母大写
            int count = 0;
            for (int i = 1; i < len; i++) {
                if (word.charAt(i) - 97 < 0) {
                    count++;//遇到大写就加一
                }
            }

            if (count != 0 && count != num) return false;

        } else {
            //程序到这，说明首字母为小写，并且这个单词长度大于等于2，若再出现大写就报错
            for (int i = 1; i < len; i++) {
                if (word.charAt(i) - 97 < 0) {
                    return false;
                }
            }

        }


        return true;
    }

    public boolean detectCapitalUse2(String word) {
        if (word.toUpperCase().equals(word)) return true;
        if (word.toLowerCase().equals(word)) return true;
        //下面就判断是不Happy这种，首为大写其余均为小写的类型

        int len = word.length(), k = 1;
        if (Character.isUpperCase(word.charAt(0))) {
            while (k < len && Character.isLowerCase(word.charAt(k))) k++;
        }

        return k == len;
    }


    public static void main(String[] args) {
        t20211114 t20211114 = new t20211114();
        System.out.println(t20211114.detectCapitalUse2("happy"));
    }


}

//677.键值映射
class MapSum {

    int[][] trie;
    int[] hash;
    int index;


    public MapSum() {
        trie = new int[2510][26];
        hash = new int[2510];

    }

    public void insert(String key, int val) {
        int p = 0;
        for (int i = 0; i < key.length(); i++) {
            int u = key.charAt(i) - 'a';
            if (trie[p][u] == 0) trie[p][u] = ++index;
            p = trie[p][u];
        }
        hash[p] = val;
    }

    public int sum(String prefix) {
        int p = 0;
        for (int i = 0; i < prefix.length(); i++) {
            int u = prefix.charAt(i) - 'a';
            if (trie[p][u] == 0) return 0;
            p = trie[p][u];
        }
        return dfs(p);
    }

    private int dfs(int p) {
        int ans = hash[p];
        for (int k = 0; k < 26; k++) {
            if (trie[p][k] != 0) ans += dfs(trie[p][k]);
        }
        return ans;
    }


}